package controller

import (
	"class_bff/class"
	"class_bff/pkg"
	"context"
	"github.com/gin-gonic/gin"
	"net/http"
)

// es搜索列表
func ClassListByEs(c *gin.Context) {
	title := c.Query("title")
	if title == "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "标题不能为空",
			"data": nil,
		})
		return
	}
	offset, limit := pkg.Pages(c)
	//远程连接class_rpc
	client := pkg.Comment()
	es, err := client.ClassListByEs(context.Background(), &class.ClassListByEsRequest{
		Title:  title,
		Offset: int64(offset),
		Limit:  int64(limit),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "查询es列表失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "查询成功",
		"data": map[string]interface{}{
			"nums": es.Nums,
			"list": es.ClassInfo,
		},
	})
	return
}
